package com.ehotting.eaf.security.service;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Map.Entry;

@Service
@ConfigurationProperties(prefix = "sms.qq")
public class SendSmsService {

	private static final Logger logger = LoggerFactory.getLogger(SendSmsService.class);
	
	private String sdkappid;
	private String appkey;
	private String smsUrl;
	private String jsonContent;
	private String isSmsTest;

	public boolean sendSMS(String template, Map<String, String> paramMap, String phone) {
		
		boolean flag = false;

		
		if("1".equals(isSmsTest)){
			logger.info("sms model:test");
    		return true;
		}
		
		if(StringUtils.isEmpty(sdkappid)){
			logger.error("sdkappid is null,pls set in file grg_qq_sms.properties");
			return false;
		}
		if(StringUtils.isEmpty(appkey)){
			logger.error("appkey is null,pls set in file grg_qq_sms.properties");
			return false;
		}
		if(StringUtils.isEmpty(smsUrl)){
			logger.error("smsUrl is null,pls set in file grg_qq_sms.properties");
			return false;
		}
		if(StringUtils.isEmpty(template)){
			logger.error("template is null,pls set in file grg_qq_sms.properties");
			return false;
		}

		if(StringUtils.isEmpty(jsonContent)){
			logger.error("jsonContent is null,pls set in file grg_qq_sms.properties");
			return false;
		}
		
		for(Entry<String,String> entry : paramMap.entrySet()){
			template = template.replaceAll("\\{"+entry.getKey()+"\\}", entry.getValue());
		}
		
		
		String sig = MD5Util.stringToMD5(appkey + phone);
		String url = smsUrl.replaceAll("\\%random\\%", String.valueOf(Math.random())).replaceAll("\\$sdkappid\\$", sdkappid);
		String smsContent = jsonContent.replaceAll("\\$phone\\$", phone).replaceAll("\\$msg\\$", template).replaceAll("\\$sig\\$", sig);
		JSONObject request = JSON.parseObject(smsContent);
		long startTime = System.currentTimeMillis();
		try {
			String response = HttpUtil.post(url, smsContent);
			JSONObject responseJsonObject = JSON.parseObject(response);
			request.putAll(responseJsonObject);
			if ("0".equals(responseJsonObject.get("result"))) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.put("result", "-1");
			request.put("errmsg", e.getMessage());
		} finally {
			request.put("costTime", System.currentTimeMillis() - startTime);
			request.put("requestUrl", url);
			logger.info(request.toString());
		}
		return flag;
	}

	
	public void setSdkappid(String sdkappid) {
		this.sdkappid = sdkappid;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}

	public void setSmsUrl(String smsUrl) {
		this.smsUrl = smsUrl;
	}

	public void setJsonContent(String jsonContent) {
		this.jsonContent = jsonContent;
	}

	public void setIsSmsTest(String isSmsTest) {
		this.isSmsTest = isSmsTest;
	}



	
	
	
	

}
